<script>
import CardItems from '@/components/CardItems/index.vue'
import { cardConfig } from './config/cardConfig'
import { popConfig } from './config/popConfig'
import { headerConfig } from './config/tableConfig'
import { selectConfig } from './config/selectConfig'
import api from '@/api/apis/sceneRoom'
import { parseSceneData } from '@/utils/scene-data'

export default {
  name: 'DataFilling',
  components: { CardItems },
  data() {
    return {
      selectList: selectConfig['dataFilling']
    }
  },
  computed: {
    /**
     * 卡片配置
     */
    cardConfig() {
      return cardConfig['dataFilling'] || {}
    },
    /**
     * 弹窗配置
     */
    popConfig() {
      return popConfig['dataFilling'] || {}
    },
    /**
     * 筛选配置
     */
    selectConfig() {
      return this.selectList || []
    },
    /**
     * 表头配置
     */
    headerConfig() {
      return headerConfig['dataFilling'] || []
    }
  },
  methods: {
    selectChange(selectData, prop, selectList) {
      if (prop === 'sceneProjectId') {
        this.getRoomList(selectData[prop], selectList, selectData)
      } else if (prop === 'sceneInfo') {
        const infos = selectData[prop]
        if (infos && infos.length > 0) {
          selectData.sceneEdificeId = infos[0] || undefined
          selectData.sceneFloorId = infos[1] || undefined
          selectData.sceneRoomId = infos[2] || undefined
        }
      }
    },
    async getRoomList(sceneProjectId, selectList, selectData) {
      this.selectList = JSON.parse(JSON.stringify(selectList))
      const res = await api.queryRoomList({
        sceneProjectId,
        sortColumn: 'floorName',
        sortOrder: 'asc'
      })
      const roomList = parseSceneData(res.data)
      const configItem = selectList[3]
      configItem.optionList = roomList
      this.$set(this.selectList, 3, {
        ...configItem
      })
      this.$refs.cardItemsRef.setSelectModuleValue({
        ...selectData
      })
    }
  }
}
</script>

<template>
  <div class="data-fill">
    <div class="CardItems-box">
      <CardItems
        ref="cardItemsRef"
        :card-config="cardConfig"
        :pop-config="popConfig"
        :select-config="selectConfig"
        :header-config="headerConfig"
        :dynamic-select-list="selectList"
        @selectChange="selectChange"
      />
    </div>
  </div>
</template>
<style lang="scss" scoped>
.data-fill {
  display: flex;
  width: 100%;
  height: 100%;

  .CardItems-box {
    flex: 1;

  }
}
</style>
